using Microsoft.AspNetCore.Mvc;
using TMS.Application.Common.Interface;
using TMS.Application.Utils;
using TMS.Domain.Entity;
namespace TMS.Api.Controllers
{

    public class Method : ControllerBase
    {
        //如果需要学生的连表的话请使用这个
        //  Method method = new Method(
        //      _IdentityService,
        //      _College,
        //      _Major,
        //       _Class,
        //     _Students
        //      );
        //学生的连表
        //  var StudentList = method.StudentList().ToList();
        //学院的连表
        // var StudentList = method.CollegeList().ToList();
        private readonly IRepository<College> _College;

        private readonly IRepository<Major> _Major;
        private readonly IRepository<Class> _Class;
        private readonly IIdentityService _IdentityService;

        private readonly IRepository<Students> _Students;

        public Method(
            IIdentityService IdentityService,
            IRepository<College> College,
            IRepository<Major> Major,
             IRepository<Class> Class,
            IRepository<Students> Students
        )
        {
            _Class = Class;
            _Major = Major;
            _College = College;
            _Students = Students;
            _IdentityService = IdentityService;
        }

        public class StudentLists
        {

            /// <summary>
            /// 学生表的Id
            /// </summary>
            /// <value></value>
            public Guid? Id { get; set; }
            /// <summary>
            /// 学号
            /// </summary>
            /// <value></value>
            public string? StudentId { get; set; }


            /// <summary>
            /// 班级id
            /// </summary>
            /// <value></value>
            public Guid ClassId { get; set; }

            /// <summary>
            /// 宿舍id
            /// </summary>
            /// <value></value>
            public Guid DormId { get; set; }

            //专业id
            public Guid MajorId { get; set; }

            //学院id
            public Guid CollegeId { get; set; }

            //学期
            public int Semester { get; set; }


        }

        /// <summary>
        /// 学生列表有id，名字，性别，身份证号，手机号，学院id，专业id，班级id， 
        /// </summary>
        /// <returns></returns>
        public List<StudentLists> StudentList()
        {
            //新建一个学生列表装id，名字，性别，身份证号，手机号，学院id，专业id，班级id，宿舍id 
            List<StudentLists> StudentLists = new List<StudentLists>();
            //学院列表
            var College = _College.Table.ToList();
            //专业列表
            var Major = _Major.Table.ToList();
            //班级列表
            var Class = _Class.Table.ToList();
            //学生列表
            var Students = _Students.Table.ToList();


            Students.ForEach((item) =>
            {
 
                try
                {
                    Class StudentsClass = Class.Where(x => x.Id == item.ClassId).FirstOrDefault();
                    Major StudentsMajor = Major.Where(x => x.Id == StudentsClass.MajorId).FirstOrDefault();
                    var StudentsCollege = College.Where(x => x.Id == StudentsMajor.CollegeId).FirstOrDefault();
                    //学生班级的Id
                    var MajorId = StudentsMajor.Id;
                    //学生学院的Id
                    var CollegeId = StudentsCollege.Id;
                    //计算学生学期
                    var DateTime = System.DateTime.Now;
                    int Semester = (((item.CreatedAt.Year - DateTime.Year) * 12 + (item.CreatedAt.Month - DateTime.Month)) + 2) / 6;

                    StudentLists.Add(new StudentLists
                    {
                        Id = item.Id,
                        StudentId = item.StudentId,
                        ClassId = item.ClassId,
                        DormId = item.DormId,
                        MajorId = MajorId,
                        CollegeId = CollegeId,
                        Semester = Semester
                    });

                }
                catch (System.Exception)
                {
                    _Students.Delete(item.Id);
                }


            });

            return StudentLists.ToList();
        }


        /// <summary>
        /// 专业列表里面带有所有的班级
        /// </summary>
        public class MajorLists
        {
            /// <summary>
            /// 专业ID
            /// </summary>
            /// <value></value>
            public Guid MajorId { get; set; }

            /// <summary>
            ///  专业名称
            /// </summary>
            /// <value></value>
            public string? MajorName { get; set; }

            /// <summary>
            /// 班级列表
            /// </summary>
            /// <value></value>
            public List<Class> Class { get; set; }
        }

        /// <summary>
        /// 使用学院列表里面带有使用专业，专业里带有使用学生
        /// </summary>
        public class CollegeLists
        {
            /// <summary>
            /// 学院ID
            /// </summary>
            /// <value></value>
            public Guid CollegeId { get; set; }

            /// <summary>
            ///  学院名称
            /// </summary>
            /// <value></value>
            public string? CollegeName { get; set; }

            /// <summary>
            /// 学年
            /// </summary>
            /// <value></value>
            public int SchoolYear { get; set; }

            /// <summary>
            /// 专业列表
            /// </summary>
            /// <value></value>
            public List<MajorLists> MajorLists { get; set; }
        }


        /// <summary>
        /// 学院列表里面有学院中的专业,专业中的班级
        /// </summary>
        /// <returns></returns>
        public List<CollegeLists> CollegeList()
        {
            //创建一份新的学院列表
            List<CollegeLists> CollegeLists = new List<CollegeLists>();

            //学院列表
            var College = _College.Table.ToList();
            //专业列表
            var Major = _Major.Table.ToList();
            //班级列表
            var Class = _Class.Table.ToList();
            //学生列表
            var Students = _Students.Table.ToList();


            foreach (var item in College)
            {
                var College_Major = Major.Where(x => x.CollegeId == item.Id).ToList();
                //创建一份新的专业列表
                List<MajorLists> MajorLists = new List<MajorLists>();

                foreach (var items in College_Major)
                {
                    var College_Major_Class=Class.Where(x=>x.MajorId==items.Id).ToList();
                    MajorLists.Add(new MajorLists
                    {
                        MajorId=items.Id,
                        MajorName=items.MajorName,
                        Class=College_Major_Class
                    });
                }
                
                CollegeLists.Add(
                    new CollegeLists
                    {
                        CollegeId = item.Id,
                        CollegeName = item.CollegeName,
                        MajorLists = MajorLists,
                        SchoolYear=item.CreatedAt.Year
                    }
                );
            }

            return CollegeLists;
        }



    }
}